//
//  HRZMyCenterViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/8.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZMyCenterViewController.h"

// Controllers
#import "HRZReceivingAddressViewController.h"   //  收货地址
#import "HRZSettingViewController.h"            //  设置
#import "HRZMyOrderViewController.h"            //  我的订单
#import "HRZCreateStoreViewController.h"
// Models
#import "HRZGridItem.h"
// Views
#import "HRZMyCenterHeaderView.h"

#import "HRZCenterItemCell.h"
#import "HRZCenterServiceCell.h"
#import "HRZUserMgCell.h"
// Vendors
#import <MJExtension.h>
// Categories
#import "UIView+SPExtension.h"
// Others
#import "HRZGlobalNetManager.h"
#import "HRZSpeedy.h"

@interface HRZMyCenterViewController ()
<UITableViewDelegate,UITableViewDataSource, HRZDelegateTableViewCellDelegate>

/* headView */
@property (strong , nonatomic)HRZMyCenterHeaderView *headView;
/* 头部背景图片 */
@property (nonatomic, strong) UIImageView *headerBgImageView;
/* tableView */
@property (strong , nonatomic)UITableView *tableView;

/* 服务数据 */
@property (strong , nonatomic)NSMutableArray<HRZGridItem *> *serviceItem;

@end

static NSString * const HRZCenterItemCellID = @"HRZCenterItemCell";
static NSString * const HRZCenterServiceCellID = @"HRZCenterServiceCell";
static NSString * const HRZUserMgCellID = @"HRZUserMgCell";

@implementation HRZMyCenterViewController

@synthesize collectionView = _collectionView;
@synthesize tableView = _tableView;

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZBottomTabH);

        [self.view addSubview:_tableView];
        
        [_tableView registerClass:[HRZCenterItemCell class] forCellReuseIdentifier:HRZCenterItemCellID];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZCenterServiceCell class]) bundle:nil] forCellReuseIdentifier:HRZCenterServiceCellID];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZUserMgCell class]) bundle:nil] forCellReuseIdentifier:HRZUserMgCellID];
        
    }
    return _tableView;
}

- (UIImageView *)headerBgImageView{
    if (!_headerBgImageView) {
        _headerBgImageView = [[UIImageView alloc] init];
        NSInteger armNum = [HRZSpeedy hrz_GetRandomNumber:1 to:9];
        [_headerBgImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"mine_main_bg_%zd",armNum]]];
        [_headerBgImageView setBackgroundColor:[UIColor greenColor]];
        [_headerBgImageView setContentMode:UIViewContentModeScaleAspectFill];
        [_headerBgImageView setClipsToBounds:YES];
    }
    return _headerBgImageView;
}

- (HRZMyCenterHeaderView *)headView
{
    if (!_headView) {
        _headView = [HRZMyCenterHeaderView sp_viewFromXib];
        _headView.frame =  CGRectMake(0, 0, kScreenWidth, 200);
    }
    return _headView;
}


- (NSMutableArray<HRZGridItem *> *)serviceItem
{
    if (!_serviceItem) {
        _serviceItem = [NSMutableArray array];
    }
    return _serviceItem;
}

#pragma mark - LifeCyle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpBase];
    
    [self setUpData];
    
    [self setUpHeaderCenterView];
}

#pragma mark - 获取数据
- (void)setUpData
{
    _serviceItem = [HRZGridItem mj_objectArrayWithFilename:@"MyServiceFlow.plist"];
}


#pragma mark - initialize
- (void)setUpBase {
    
    self.view.backgroundColor = HRZBGColor;
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.tableFooterView = [UIView new]; //去除多余分割线
    self.tableView.estimatedRowHeight = 150;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - 初始化头部
- (void)setUpHeaderCenterView{
    
    self.tableView.tableHeaderView = self.headView;
    self.headerBgImageView.frame = self.headView.bounds;
    [self.headView insertSubview:self.headerBgImageView atIndex:0]; //将背景图片放到最底层
    WEAKSELF
    //  点击头像
    self.headView.headClickBlock = ^{
        
    };
    //  点击设置
    self.headView.setClickBlock = ^{
        HRZSettingViewController *dcSetVc = [HRZSettingViewController new];
        [weakSelf.navigationController pushViewController:dcSetVc animated:YES];
    };
}


#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 2;
    } else {
        return 1;
    }
   
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cusCell = [UITableViewCell new];
    if (indexPath.section == 0) {
        HRZCenterItemCell *cell = [tableView dequeueReusableCellWithIdentifier:HRZCenterItemCellID forIndexPath:indexPath];
        cell.delegate = self;
        
        cusCell = cell;
    }else if(indexPath.section == 1){
        HRZCenterServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:HRZCenterServiceCellID forIndexPath:indexPath];
        cell.serviceItemArray = [NSMutableArray arrayWithArray:_serviceItem];
        cell.delegate = self;
        
        cusCell = cell;
    }else if (indexPath.section == 2){
        HRZUserMgCell *cell = [tableView dequeueReusableCellWithIdentifier:HRZUserMgCellID forIndexPath:indexPath];
        cell.titleContent = indexPath.item == 0 ? @"我的地址": @"门店注册";
        cusCell = cell;
    }
    
    return cusCell;
}

#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            HRZReceivingAddressViewController *add = [HRZReceivingAddressViewController new];
            [self.navigationController pushViewController:add animated:YES];
        } else {
            UIStoryboard *Sb = [UIStoryboard storyboardWithName:@"store" bundle:nil];
            
            HRZCreateStoreViewController *stroe = [Sb instantiateViewControllerWithIdentifier:@"HRZCreateStoreViewController"];
            [self.navigationController pushViewController:stroe animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//待付款Item组
        return 90;
    }else if (indexPath.section == 1){
        return 215;
    }else if (indexPath.section == 2){
        return 50;
    }else if (indexPath.section == 3){
        return 200;
    }
    return 0;
}

#pragma mark - 滚动tableview 完毕之后
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //图片高度
    CGFloat imageHeight = self.headView.sp_height;
    //图片宽度
    CGFloat imageWidth = kScreenWidth;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    //上移
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        self.headerBgImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
}

#pragma mark - -------<HRZDelegateTableViewCellDelegate>------
- (void)delegateTableCell:(UITableViewCell *)cell didSelectedItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *tabelIndex = [_tableView indexPathForCell:cell];
    if (tabelIndex.section == 0) {
        HRZMyOrderViewController * myOrderVC = [[HRZMyOrderViewController alloc] init];
        
        myOrderVC.status = indexPath.item;
        
        [self.navigationController pushViewController:myOrderVC animated:YES];
    }
    NSLog(@"%ld==%ld", (long)tabelIndex.section, indexPath.row);
}


@end
